package com.ygj.inspection.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.ygj.inspection.service.EmployeeService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.base.common.json.JsonUtil;
import com.base.common.persistent.dao.EmployeeMapper;
import com.base.common.persistent.model.Employee;
import com.base.common.utils.EasyUtil;
import com.cjy.common.bean.Files;
import com.gexin.rp.sdk.base.uitls.Base64Util;

@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {

	@Resource
	private EmployeeMapper employeeMapper;
	
	@Resource
	private EasyUtil easyUtil;

	@Override
	public List<Employee> getEmployeesByDeptAndCompoundsId(int dept,
			int compoundsId) {
		return employeeMapper.queryEmployeesByDeptAndCompoundsId(dept,
				compoundsId);
	}

	@Override
	public int chooseDefaultCompounds(int employeeId, int compoundsId) {
		return employeeMapper.updateChooseDefaultCompounds(employeeId,
				compoundsId);
	}

	@Transactional
	@Override
	public int subHeadPic(int employeeId, Files files, String basePath) {
		int result = 0;
		
		// 图片相对保存地址
		String sysImagesFile = "/resources/images/employee_head_img" + "/" + employeeId;

		// 保存图片
		String[] fileNames = files.getFileFileName();
		String picUrls = "";
		try {
			if (fileNames != null && fileNames.length != 0) {
				picUrls = easyUtil.savePictures(files.getFile(), basePath,
						sysImagesFile, fileNames, picUrls);
			}
			if (picUrls != "") {
				result = employeeMapper.updateEmployeeHeadPicUrl(employeeId, picUrls);
			}
		} catch (IOException e) {
			e.printStackTrace();
			return 0;
		}
		return result;
	}

	@Override
	public String getHeadPicUrlByMySelf(int employeeId) {
		return employeeMapper.getHeadPicUrlByEmployeeId(employeeId);
	}

	/* (non-Javadoc)
	 * @see EmployeeService#getYunWorkEmployeeByCondition(java.util.Map)
	 */
	@Override
	public Object getYunWorkEmployeeByCondition(Map map) {
		String name = (String) map.get("name");
		if(EasyUtil.isChinese(name)){
			map.put("name", easyUtil.hanziToPinyin(name));
		}
		return employeeMapper.getYunWorkEmployeeByCondition(map);
	}

	/* (non-Javadoc)
	 * @see EmployeeService#getEmployeesByUncertainName(int, java.lang.String)
	 */
	@Override
	public Object getEmployeesByUncertainName(int compoundsId, String name, int dept) {
		if(EasyUtil.isChinese(name)){
			name = easyUtil.hanziToPinyin(name);
		}
		return employeeMapper.queryEmployeesByCompoundsIdAndName(compoundsId, name, dept);
	}

	/* (non-Javadoc)
	 * @see EmployeeService#getEmchatUserInfo(int)
	 */
	@Override
	public Object getEmchatUserInfo(String ids) {
		List infos = new ArrayList();
		String[] idArr = ids.split(",");
		for(int i = 0; i < idArr.length; i++){
			Map map = new HashMap();
			Employee employee = employeeMapper.queryEmployeeById(Integer.parseInt(idArr[i]));
			map.put("username", idArr[i]);
			map.put("nickname", employee.getName());
			map.put("headPicUrl", employee.getHeadPicUrl());
			infos.add(map);
		}
		return infos;
	}

	@Override
	public Object getFaceData(String id) {
		String faceData = new String();
		faceData = employeeMapper.getFaceData(Integer.valueOf(id));
		Map<String, String> result = new HashMap<>();
		result.put("faceData", faceData);
		return result;
	}
	
	@Override
	public Object updateFaceData(String ids, String faceData) {
		if(StringUtils.isNotBlank(ids)) {
			employeeMapper.updateFaceDataById(Integer.valueOf(ids), faceData);
			return 1;
		}
		return 0;
	}
	
}
